package com.example.lemonhealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemonhealth.dao.TCheckitemMapper;
import com.example.lemonhealth.entity.TCheckitem;
import com.example.lemonhealth.service.CheckitemService;
import com.example.lemonhealth.utils.QueryPageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Lemon
 * @Description: 检查项管理相关接口
 * @Date: 2024/12/24 11:00
 */
@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Resource
    private TCheckitemMapper tCheckitemMapper;

    /**
     * @Author Lemon
     * @Description 分页查询-检查项管理
     * @Date 2024/12/24 11:12
     * @Param [queryPageBean]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.example.lemonhealth.entity.TCheckitem>
     */
    @Override
    public IPage<TCheckitem> findPage(QueryPageBean queryPageBean) {
        Page<TCheckitem> page = new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<TCheckitem> productQuery = Wrappers.lambdaQuery();
        Page<TCheckitem> tCheckitemPage = tCheckitemMapper.selectPage(page, productQuery);
        IPage<TCheckitem> iTCheckitemPage = new Page<>();
        BeanUtils.copyProperties(tCheckitemPage, iTCheckitemPage);
        List<TCheckitem> productVoList = tCheckitemPage.getRecords().stream().map(
                e -> product2ProductVo(e)
        ).collect(Collectors.toList());
        iTCheckitemPage.setRecords(productVoList);
        iTCheckitemPage.setTotal(tCheckitemPage.getTotal());
        return iTCheckitemPage;
    }

    /**
     * @Author Lemon
     * @Description 根据id查询检查项（数据回显）
     * @Date 2024/12/24 14:26
     * @Param [id]
     * @Return com.example.lemonhealth.utils.Result
     */
    @Override
    public TCheckitem findById(Integer id) {
        return tCheckitemMapper.selectById(id);
    }

    /**
     * @Author Lemon
     * @Description 保存修改-检查项管理
     * @Date 2024/12/24 15:33
     * @Param [tCheckitem]
     * @Return com.example.lemonhealth.utils.Result
     */
    @Override
    public void edit(TCheckitem tCheckitem) {
        tCheckitemMapper.updateById(tCheckitem);
    }

    /**
     * @Author Lemon
     * @Description 查询所有检查项
     * @Date 2024/12/24 17:44
     * @Param []
     * @Return java.lang.Object
     */
    @Override
    public Object findAll() {
        List<TCheckitem> tCheckitems = tCheckitemMapper.selectList(null);
        return tCheckitems;
    }

    /**
     * @Author Lemon
     * @Description 添加检查项
     * @Date 2024/12/25 14:11
     * @Param [tCheckitem]
     * @Return void
     */
    @Override
    public void addCheckitem(TCheckitem tCheckitem) {
        tCheckitemMapper.insert(tCheckitem);
    }

    /**
     * @Author Lemon
     * @Description 删除检查项
     * @Date 2024/12/25 14:33
     * @Param [id]
     * @Return void
     */
    @Override
    public void deleteCheckitem(Integer id) {
        tCheckitemMapper.deleteById(id);
    }

    private TCheckitem product2ProductVo(TCheckitem product) {
        TCheckitem productVo = new TCheckitem();
        BeanUtils.copyProperties(product, productVo);
        return productVo;
    }
}
